package com.wxy.ideaplugin.utils;


import javax.swing.*;
import java.util.Arrays;
import java.util.List;

public class SqlExecThread extends Thread{
    private JTextArea aiLog_textArea;
    private JTextArea execlog_textArea;

    public SqlExecThread(JTextArea aiLog_textArea, JTextArea execlog_textArea) {
        this.aiLog_textArea = aiLog_textArea;
        this.execlog_textArea = execlog_textArea;
    }

    public void run() {
        JScrollPane scrollPane1 = (JScrollPane)execlog_textArea.getParent().getParent();
        JScrollBar scrollBar=scrollPane1.getVerticalScrollBar();
        String sqls = aiLog_textArea.getText().toString();
        //jdbc
        List<String> sqlList = Arrays.asList(sqls.split(";\n"));
        execlog_textArea.append("#------- 模型生成开始 -------;\n");
        for (String sql : sqlList) {
            if (sql.trim().startsWith("#")) {
                continue;
            }
            if (sql.trim().equalsIgnoreCase("")) {
                continue;
            }
            JdbcUtils.executeUpdate(sql);
            sql = sql.replaceAll("\n", "");
            if (sql.length()>150) {
                execlog_textArea.append("#" + sql.substring(0, 150) + "...;\n");
            }else {
                execlog_textArea.append("#" + sql + ";\n");
            }
            //滚动尾部
            scrollBar.setValue(scrollBar.getMaximum());
        }
        //滚动尾部
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        execlog_textArea.append("#------- 模型执行完成 -------;\n\n");
        scrollBar.setValue(scrollBar.getMaximum());
    }



}
